<xsl:stylesheet xmlns:x="http://www.w3.org/2001/XMLSchema"
    version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:cmswrt="http://schemas.microsoft.com/WebPart/v3/Publishing/runtime"
	  xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd"
    exclude-result-prefixes="xsl cmswrt x">
    <xsl:output method="xml" indent="no" />
    <xsl:param name="SiteId" />
    <xsl:param name="FeedTitle" />
    <xsl:param name="FeedDescription" />
    <xsl:param name="PageUrl" />
    <xsl:param name="BlankTitle" />
    <xsl:param name="UseCopyUtil" />
    
    <xsl:template match="/">
        <xsl:call-template name="OuterTemplate" />
    </xsl:template>
    
    <xsl:template name="OuterTemplate">
        <xsl:variable name="Rows" select="/dsQueryResponse/Rows/Row" />
        <xsl:variable name="RowCount" select="count($Rows)" />
        <xsl:variable name="IsEmpty" select="$RowCount = 0" />
        
	  <rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" version="2.0">
	    <channel>
        <title><xsl:value-of select="$FeedTitle"/></title> 
	      <xsl:value-of select="'&lt;link&gt;'" disable-output-escaping="yes" />
        <xsl:value-of select="$PageUrl"/>
        <xsl:value-of select="'&lt;/link&gt;'" disable-output-escaping="yes" />    
	      <description><xsl:value-of select="$FeedDescription"/></description>
		    <itunes:summary>
			    <xsl:value-of select="$FeedDescription"/>
		    </itunes:summary>
		    <ttl>60</ttl>
		    <category>Waiting for Tags to be implemented</category>
		    <itunes:category>Waiting for Tags to be implemented</itunes:category>

    		<xsl:for-each select="$Rows">
          <item>
            <title>
              <xsl:if test="string-length(@Title) != 0">
                <xsl:value-of select="@Title" />
              </xsl:if>

              <xsl:if test="string-length(@Title) = 0">
                Blank Title
              </xsl:if>
            </title>

            <link>
              <xsl:call-template name="OuterTemplate.GetSafeLink">
                <xsl:with-param name="Url" select="@LinkUrl" />
              </xsl:call-template>
            </link>

            <description>
              <xsl:if test="string-length(@Description) != 0">
                <xsl:value-of disable-output-escaping="no" select="@Description" />
              </xsl:if>
            </description>

            <itunes:summary>
              <xsl:if test="string-length(@Description) != 0">
                <xsl:value-of disable-output-escaping="no" select="@Description" />
              </xsl:if>
            </itunes:summary>

            <category>
              <xsl:value-of select="@Tags"/>
            </category>
            <itunes:category>
              <xsl:value-of select="@Tags"/>
            </itunes:category>

            <itunes:explicit>No</itunes:explicit>

            <pubDate>
              <xsl:value-of disable-output-escaping="no" select="@PubDate" />
            </pubDate>

            <managingEditor>
              <xsl:if test="string-length(@Author) != 0">
                <xsl:value-of select="substring-after(@Author, '\')" disable-output-escaping="no" />
              </xsl:if>
              <xsl:if test="string-length(@Author) = 0">
                <xsl:value-of select="substring-after(@CreatedBy, '\')" disable-output-escaping="no" />
              </xsl:if>
            </managingEditor>
            <itunes:author>
              <xsl:if test="string-length(@Author) != 0">
                <xsl:value-of select="substring-after(@Author, '\')" disable-output-escaping="no" />
              </xsl:if>
              <xsl:if test="string-length(@Author) = 0">
                <xsl:value-of select="substring-after(@CreatedBy, '\')" disable-output-escaping="no" />
              </xsl:if>
            </itunes:author>

            <itunes:duration>
              <xsl:call-template name="ConvertDurationToFormattedString">
                <xsl:with-param name="Duration" select="@Duration" />
              </xsl:call-template>
            </itunes:duration>

            <xsl:element name="enclosure">
              <xsl:attribute name="url">
                <xsl:value-of select="@LinkUrl"/>
              </xsl:attribute>
              <xsl:attribute name="length">
                <xsl:value-of select="@LengthInBytes"/>
              </xsl:attribute>
              <xsl:attribute name="type">
                <xsl:call-template name="GetMediaType">
                  <xsl:with-param name="LinkUrl" select="@LinkUrl"/>
                </xsl:call-template>
              </xsl:attribute>
            </xsl:element>
          </item>
        </xsl:for-each>
  	  </channel>
    </rss>
  </xsl:template>
  <xsl:template name="OuterTemplate.GetSafeLink">
    <xsl:param name="Url"/>
    <xsl:if test="$UseCopyUtil = 'True'">
      <xsl:call-template name="OuterTemplate.GetSafeStaticUrl">
        <xsl:with-param name="Url" select="concat('/_layouts/CopyUtil.aspx?Use=id&amp;Action=dispform&amp;ItemId=',@ID,'&amp;ListId=',@ListId,'&amp;WebId=',@WebId,'&amp;SiteId=',$SiteId)"/>
      </xsl:call-template>
    </xsl:if>
    <xsl:if test="$UseCopyUtil != 'True'">
        <xsl:call-template name="OuterTemplate.GetSafeStaticUrl">
            <xsl:with-param name="Url" select="$Url"/>
        </xsl:call-template>
    </xsl:if>
  </xsl:template>
  <xsl:template name="OuterTemplate.GetSafeStaticUrl">
    <xsl:param name="Url"/>
    <xsl:value-of select="cmswrt:EnsureIsAllowedProtocol($Url)"/>
  </xsl:template>
  <!-- 
  This function converts a number (the duration) into
  a '00:00:00' formatted string 
  -->
  <xsl:template name="ConvertDurationToFormattedString">
    <xsl:param name="Duration"/>
    <xsl:variable name="duration-number">
      <xsl:number value="$Duration" />
    </xsl:variable>
    <xsl:variable name="hours" select="$duration-number div 3600"/>
    <xsl:variable name="minutes" select ="($duration-number mod 3600) div 60" />
    <xsl:variable name="seconds" select ="$duration-number mod 60" />
    <xsl:value-of select="format-number(floor($hours),'00')"/>:<xsl:value-of select="format-number(floor($minutes),'00')"/>:<xsl:value-of select="format-number($seconds, '00')"/>
  </xsl:template>

  <!-- 
  This function returns the media type based on the
  file extension.                                   
  -->
  <xsl:template name="GetMediaType">
    <xsl:param name = "LinkUrl"/>
    <xsl:variable name="extension">
      <xsl:call-template name="GetFileExtension">
        <xsl:with-param name="LinkUrl" select="$LinkUrl" />
      </xsl:call-template>
    </xsl:variable>
    <xsl:choose>
      <xsl:when test="$extension = 'mp3'">
        <xsl:text>audio/mpeg</xsl:text>
      </xsl:when>
      <xsl:when test="$extension = 'm4a'">
        <xsl:text>audio/x-m4a</xsl:text>
      </xsl:when>
      <xsl:when test="$extension = 'mp4'">
        <xsl:text>video/mp4</xsl:text>
      </xsl:when>
      <xsl:when test="$extension = 'm4v'">
        <xsl:text>video/x-m4v</xsl:text>
      </xsl:when>
      <xsl:when test="$extension = 'mov'">
        <xsl:text>video/quicktime</xsl:text>
      </xsl:when>
      <xsl:when test="$extension = 'pdf'">
        <xsl:text>application/pdf</xsl:text>
      </xsl:when>
      <xsl:otherwise>
        <xsl:text>Unknown</xsl:text>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <!-- 
  This function returns the file extension by finding the
  characters after the last '.' character.
  -->
  <xsl:template name='GetFileExtension'>
    <xsl:param name='LinkUrl'/>
    <xsl:variable name='notDoneYet' select='contains($LinkUrl,".")'/>
    <xsl:choose>
      <xsl:when test='$notDoneYet'>
        <xsl:call-template name='GetFileExtension'>
          <xsl:with-param name='LinkUrl'
            select='substring-after($LinkUrl,".")'/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select='$LinkUrl'/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

</xsl:stylesheet>
